package com.fengye.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengye.api.service.ArticleService;
import com.fengye.common.constants.CommonConstant;
import com.fengye.common.utils.BeanCopyUtils;
import com.fengye.common.domain.MyPage;
import com.fengye.common.domain.entity.Article;
import com.fengye.common.domain.entity.Category;
import com.fengye.common.domain.vo.ArticleListVo;
import com.fengye.common.domain.vo.ArticleDetailVo;
import com.fengye.common.domain.vo.HotArticleVo;
import com.fengye.api.mapper.ArticleMapper;
import com.fengye.api.service.CategoryService;

import com.fengye.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author sunlei
* @description 针对表【fy_article】的数据库操作Service实现
* @date  2024-02-25 01:00:31
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService {

    @Resource
    @Lazy
    private CategoryService categoryService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<HotArticleVo> hotArticleList() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        // 必须是正式发布的文章
        queryWrapper.eq(Article::getStatus, CommonConstant.ARTICLE_STATUS_NORMAL);
        // 按照浏览量排序
        queryWrapper.orderByDesc(Article::getViewCount).last(CommonConstant.ARTICLE_HOT_LIMIT);
        List<Article> hotArticles = list(queryWrapper);

        return BeanCopyUtils.copyBeanList(hotArticles, HotArticleVo.class);
    }

    @Override
    public MyPage<ArticleListVo> articleList(Long categoryId, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有分类id
        queryWrapper.eq(Objects.nonNull(categoryId) && categoryId > 0, Article::getCategoryId, categoryId);
        // 必须是正式发布的文章
        queryWrapper.eq(Article::getStatus, CommonConstant.ARTICLE_STATUS_NORMAL);
        // 对置顶进行排序
        queryWrapper.orderByDesc(Article::getIsTop);

        // 分页查询
        Page<Article> page = new Page<Article>(pageNum, pageSize);
        Page<Article> articlePage = page(page, queryWrapper);
        // stream流中peek与map的区别及用法： https://blog.csdn.net/weixin_42218169/article/details/117357054
        List<Article> articleList = articlePage.getRecords().stream().peek(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName())).collect(Collectors.toList());

        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articleList, ArticleListVo.class);

        return new MyPage<ArticleListVo>(articleListVos, articlePage.getTotal(), articlePage.getCurrent(), articlePage.getSize());
    }

    @Override
    public ArticleDetailVo getArticleDetail(Long id) {
        Article article = getById(id);
        // 设置比起爱你名称
        Category category = categoryService.getById(article.getCategoryId());
        if (!Objects.isNull(category)) {
            article.setCategoryName(category.getName());
        }
        return BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
    }

    @Override
    public void updateViewCount(Long id) {
        redisUtils.hIncrement(CommonConstant.ARTICLE_VIEW_COUNT_REDIS_KEY, id.toString(), 1);
    }
}




